package com.sensePlusVR.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sensePlusVR.activity.mine.CoreSettingActivity;
import com.sensePlusVR.activity.mine.FeedbackActivity;
import com.sensePlusVR.activity.mine.HyperlinkSettingActivity;
import com.sensePlusVR.activity.mine.NotifiSettingActivity;
import com.sensePlusVR.activity.mine.UserGuideActivity;
import com.sensePlusVR.activity.mine.UserSettingActivity;
import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.base.BaseFragment;
import com.sensePlusVR.bean.AppVersionInfo;
import com.sensePlusVR.bean.FeedbackCountRespDto;
import com.sensePlusVR.main.R;
import com.sensePlusVR.manager.DataCenter;
import com.sensePlusVR.manager.EventListener;
import com.sensePlusVR.manager.FeedbackManager;
import com.sensePlusVR.setting.bluetooth.BlueScannerActivity;
import com.sensePlusVR.setting.bluetooth.BlueUnwrapActivity;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.MyPageJump;
import com.sensePlusVR.utils.MySharedPreferences;
import com.sensePlusVR.utils.VersionUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;


@ContentView(R.layout.fragment_account)
public class AccountFragment extends BaseFragment implements OnClickListener{
	private static final String TAG = AccountFragment.class.getSimpleName();
	private String KEY = this.getClass().getSimpleName();
	
	@ViewInject(R.id.notification_new_icon)
	private ImageView notification_new_icon;
	
	@ViewInject(R.id.main_setting_alarm)
	private ImageView main_setting_alarm;
	
	@ViewInject(R.id.setting_username)
	private TextView setting_username;
	
	@ViewInject(R.id.setting_level)
	private TextView setting_level;
	
	@ViewInject(R.id.setting_blue_unwrap_device)
	private TextView setting_blue_unwrap_device;
	
	@ViewInject(R.id.setting_relative1)
	private RelativeLayout setting_relative1;
	
	@ViewInject(R.id.setting_blue_relative)
	private RelativeLayout setting_blue_relative;
	
	@ViewInject(R.id.setting_settings_relative)
	private RelativeLayout setting_settings_relative;
	
	@ViewInject(R.id.setting_core_feedback_relative)
	private RelativeLayout setting_core_feedback_relative;
	
	@ViewInject(R.id.setting_guide_relative)
	private RelativeLayout setting_guide_relative;
	
	@ViewInject(R.id.setting_point)
	private ImageView main_settting_point;
	
	@ViewInject(R.id.setting_faq)
	private TextView setting_faq;
	
	
	private boolean canUpdate = false;
	
	private DataCenter dataCenter;
	
	EventListener listener = new EventListener(){
		@Override
		public void Notify(int event, Object data, Object Params) {
			if (Params == null || data == null)
				return;
			MyLog.d(TAG, "event = "+event);
			if (event == FeedbackManager.EventFeedbackNoReadCount){
				FeedbackCountRespDto feedbackCountRespDto = (FeedbackCountRespDto) data;
				MyLog.d(TAG, "listener data : " + feedbackCountRespDto.toString());
				Integer count = feedbackCountRespDto.getCount();
				if(notification_new_icon == null)return;
				if(count > 0){
					notification_new_icon.setVisibility(View.VISIBLE);
				}else{
					notification_new_icon.setVisibility(View.GONE);
				}
			
			}
		}
	};
	
	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyLog.d(TAG, "onCreate...");
		dataCenter = DataCenter.getInstans();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = x.view().inject(this, inflater, container);
		initView();
		MyLog.d(TAG, "onCreateView...");
		
		return view;
	}
	
	
	private void initView() {
		main_setting_alarm.setOnClickListener(this);
		setting_relative1.setOnClickListener(this);
		setting_blue_relative.setOnClickListener(this);
		setting_settings_relative.setOnClickListener(this);
		setting_core_feedback_relative.setOnClickListener(this);
		setting_guide_relative.setOnClickListener(this);
		setting_faq.setOnClickListener(this);
		main_settting_point.setVisibility(canUpdate?View.VISIBLE:View.INVISIBLE);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_setting_alarm:
			MyPageJump.pageJump(getActivity(), NotifiSettingActivity.class);
			break;
		case R.id.setting_relative1:
			MyPageJump.pageJump(getActivity(), UserSettingActivity.class);
			break;
		case R.id.setting_blue_relative:
			MyLog.d(TAG, "setting_blue_relative");
			VRApplication vrApplication = VRApplication.getInstance();
			MyLog.d(TAG, "isBTConnected : " + vrApplication.isBTConnected());
			if(!vrApplication.isBTConnected()){
				vrApplication.toBTDisconnect();
			}
			String macAdr = MySharedPreferences.getString(getActivity(), Constant.SP_BLUE_ADDRESS);
			if(macAdr!=""){
				MyPageJump.pageJump(getActivity(), BlueUnwrapActivity.class);
			}else{
				MySharedPreferences.save(getActivity(), "shouldFinish",false);
				MySharedPreferences.save(getActivity(), Constant.SP_BLUE_IS_FROM_SETTING, true);
				MyPageJump.pageJump(getActivity(), BlueScannerActivity.class);
			}
			break;
		case R.id.setting_settings_relative:
			MyPageJump.pageJump(getActivity(), CoreSettingActivity.class);
			break;
		case R.id.setting_guide_relative:
			MyPageJump.pageJump(getActivity(), UserGuideActivity.class);
			break;
		case R.id.setting_core_feedback_relative:
			MyPageJump.pageJump(getActivity(), FeedbackActivity.class);
			break;
		case R.id.setting_faq:
			MyPageJump.pageJumpAddString(getActivity(), HyperlinkSettingActivity.class, Constant.INTENT_HYPERLINK_KEY
					, Constant.INTENT_HYPERLINK_FAQ);
			break;
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();
		MyLog.d(TAG, "onResume...");
		dataCenter.addFeedbackEventListener(listener, this, KEY);
		String blueName = MySharedPreferences.getString(getActivity(),  Constant.SP_BLUE_NAME);
		MyLog.d(TAG, "blueName : " + blueName);
		setting_blue_unwrap_device.setText(blueName);
		String userName = MySharedPreferences.getString2(getActivity(),MySharedPreferences.NAME_NOT_CLEAR, Constant.LOGIN_SUCCESS_SHOWNAME);
		String level = MySharedPreferences.getString2(getActivity(),MySharedPreferences.NAME_NOT_CLEAR, Constant.LOGIN_SUCCESS_LEVEL);
		MyLog.d(TAG, "userName : " + userName + " level : " + level);
		main_setting_alarm.setOnClickListener(this);
		setting_username.setText(userName);
		setting_level.setText(level);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		MyLog.d(TAG, "onPause...");
		dataCenter.delFeedbackEventListener(KEY);
		
	}
	
	@Override
	public void onStop() {
		super.onStop();
		MyLog.d(TAG, "onStop...");
	}
	
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		MyLog.d(TAG, "onDestroyView...");
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		MyLog.d(TAG, "onDestroy...");
	}
	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		// TODO Auto-generated method stub
		super.setUserVisibleHint(isVisibleToUser);
		
		if(isVisibleToUser){
			
			checkUpdate();
			
			checkFeedbackNew();
		}
		
		
	}
	
	private void checkFeedbackNew(){
		//请求是否有未读Feedback
		if(dataCenter == null){
			dataCenter = DataCenter.getInstans();
		}
		dataCenter.getFeedbackNoReadCount();
	}
	
	//检查更新
	private void checkUpdate(){
		
		if(!canUpdate){
			
				AppVersionInfo newAppVersionInfo = VRApplication.getInstance().getNewVersionInfo();
			
			if(newAppVersionInfo!=null){

				String curVersionName = VersionUtil.getVersionName(VRApplication.getAppContext());
				
				canUpdate = VersionUtil.canVersionUpdate(newAppVersionInfo.getVersonname(), curVersionName);
							
			}		
		}
		
		MyLog.d(TAG, "canUpdate = "+canUpdate);
		
		if(main_settting_point!=null)		
			main_settting_point.setVisibility(canUpdate?View.VISIBLE:View.INVISIBLE);
	}

}
